import 'dart:convert';

import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/get_zero_model_entity.g.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:sp_util/sp_util.dart';

export 'package:flutter_deer/generated/json/get_zero_model_entity.g.dart';

@JsonSerializable()
class GetZeroModelEntity {
	int? type = 0;//能源产品模式(0:SaaS模式， 1: 零管模式 2: 副机)

	GetZeroModelEntity();

	factory GetZeroModelEntity.fromJson(Map<String, dynamic> json) => $GetZeroModelEntityFromJson(json);

	Map<String, dynamic> toJson() => $GetZeroModelEntityToJson(this);

	void save() {
		SpUtil.putObject(Constant.zeroModel, toJson());
	}

	GetZeroModelEntity? get() {
    final String? jsonString = SpUtil.getString(Constant.zeroModel);
    if (jsonString?.isNotEmpty != true) {
      return null;
    }
    try {
      final dynamic data = json.decode(jsonString!);
      if (data is Map<String, dynamic>) {
        return GetZeroModelEntity.fromJson(data);
      } else {
        print('解析失败：期望 Map<String, dynamic>，但返回的是 ${data.runtimeType}');
        return null;
      }
    } catch (e, stackTrace) {
      print('JSON 解析失败: $e\n$stackTrace');
      return null;
    }
  }

  @override
	String toString() {
		return jsonEncode(this);
	}
}